package 笔试记录.M8day19百度;

public class Mian {
    /*
    ["abcd","ab"] 求最长公共前缀
    ["abcd","acd"] 求最长公共前缀
     */
    static String res;
    static int len = 0;
    public static void main(String[] args){
        String[] arr = {"abcd","acd"};//假设数组已经设立

        for(int i = 0;i < arr.length;i++){
            if(i == arr.length - 1) break;
            for(int j = i +1;j < arr.length;i++){
                solve(arr[i],arr[j]);
            }
        }
        System.out.println(res);
    }

    public static void solve(String str1,String str2){
        char[] arr1 = str1.toCharArray();
        char[] arr2 = str2.toCharArray();
        int i = 0;
        int j = 0;

        while(i < arr1.length && j < arr2.length){
            if(arr1[i] == arr2[j]){
                i++;
                j++;
            }else{
                break;
            }
        }
        if(j > len){
            res = str1.substring(0,j + 1);
            len = j + 1;
        }
    }
}
